package com.yayun.merchant.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yayun.merchant.mapper.UmsMemberAddressMapper;
import com.yayun.merchant.domain.UmsMemberAddress;
import com.yayun.merchant.service.IUmsMemberAddressService;
import com.yayun.common.utils.SecurityUtils;
import com.yayun.common.utils.DateUtils;

/**
 * 会员收货地址Service业务层处理
 * 
 * @author yayun
 */
@Service
public class UmsMemberAddressServiceImpl implements IUmsMemberAddressService {
    @Autowired
    private UmsMemberAddressMapper umsMemberAddressMapper;

    /**
     * 查询会员收货地址
     * 
     * @param id 会员收货地址主键
     * @return 会员收货地址
     */
    @Override
    public UmsMemberAddress selectUmsMemberAddressById(Long id) {
        return umsMemberAddressMapper.selectUmsMemberAddressById(id);
    }

    /**
     * 查询会员收货地址列表
     * 
     * @param umsMemberAddress 会员收货地址
     * @return 会员收货地址
     */
    @Override
    public List<UmsMemberAddress> selectUmsMemberAddressList(UmsMemberAddress umsMemberAddress) {
        umsMemberAddress.setUserId(SecurityUtils.getUserId());
        return umsMemberAddressMapper.selectUmsMemberAddressList(umsMemberAddress);
    }

    /**
     * 新增会员收货地址
     * 
     * @param umsMemberAddress 会员收货地址
     * @return 结果
     */
    @Override
    @Transactional
    public int insertUmsMemberAddress(UmsMemberAddress umsMemberAddress) {
        umsMemberAddress.setUserId(SecurityUtils.getUserId());
        umsMemberAddress.setCreateTime(DateUtils.getNowDate());
        if (umsMemberAddress.getIsDefault() != null && umsMemberAddress.getIsDefault() == 1) {
            umsMemberAddressMapper.updateNotDefault(umsMemberAddress.getUserId());
        }
        return umsMemberAddressMapper.insertUmsMemberAddress(umsMemberAddress);
    }

    /**
     * 修改会员收货地址
     * 
     * @param umsMemberAddress 会员收货地址
     * @return 结果
     */
    @Override
    @Transactional
    public int updateUmsMemberAddress(UmsMemberAddress umsMemberAddress) {
        umsMemberAddress.setUpdateTime(DateUtils.getNowDate());
        if (umsMemberAddress.getIsDefault() != null && umsMemberAddress.getIsDefault() == 1) {
            umsMemberAddressMapper.updateNotDefault(SecurityUtils.getUserId());
        }
        return umsMemberAddressMapper.updateUmsMemberAddress(umsMemberAddress);
    }

    /**
     * 批量删除会员收货地址
     * 
     * @param ids 需要删除的会员收货地址主键
     * @return 结果
     */
    @Override
    public int deleteUmsMemberAddressByIds(Long[] ids) {
        return umsMemberAddressMapper.deleteUmsMemberAddressByIds(ids);
    }

    /**
     * 删除会员收货地址信息
     * 
     * @param id 会员收货地址主键
     * @return 结果
     */
    @Override
    public int deleteUmsMemberAddressById(Long id) {
        return umsMemberAddressMapper.deleteUmsMemberAddressById(id);
    }

    /**
     * 设置默认地址
     *
     * @param id 地址ID
     * @return 结果
     */
    @Override
    @Transactional
    public int setDefault(Long id) {
        umsMemberAddressMapper.updateNotDefault(SecurityUtils.getUserId());
        return umsMemberAddressMapper.updateDefaultById(id);
    }
} 